package com.example.dllo.keepdemo.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseActivity;
import com.example.dllo.keepdemo.dynamic.DynamicFragment;
import com.example.dllo.keepdemo.me.MeFragment;
import com.example.dllo.keepdemo.tools.ActivityCollector;
import com.example.dllo.keepdemo.tools.NoScrollViewPager;
import com.example.dllo.keepdemo.train.TrainFragment;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {
    private MyViewPagerAdapter adapter;
    private TabLayout tabLayout;
    private NoScrollViewPager viewPager;
    private ArrayList<Fragment> data;
    private static boolean isExit = false;

    Handler myhandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            isExit = false;
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout) findViewById(R.id.tablayout_main_activity);
        viewPager = (NoScrollViewPager) findViewById(R.id.viewpager_main_activity);

        Intent intent = getIntent();
        String username = intent.getStringExtra("username");
        String usericon = intent.getStringExtra("usericon");

        MeFragment meFragment = new MeFragment();
        if (username != null && usericon != null) {
            Log.d("MainActivity", username);
            Log.d("MainActivity", usericon);

            Bundle bundle = new Bundle();
            bundle.putString("username",username);
            bundle.putString("usericon", usericon);
            meFragment.setArguments(bundle);
        }


        data = new ArrayList<>();
        data.add(new TrainFragment());
        data.add(new DynamicFragment());
        data.add(meFragment);

        adapter = new MyViewPagerAdapter(getSupportFragmentManager(),data,this);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(adapter.getTabView(i));
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(this, "再次点击退出程序", Toast.LENGTH_SHORT).show();
            //利用handler延迟发送更改状态信息

        } else {
            ActivityCollector.finishAll();
            System.exit(0);
        }
    }
}
